$(function () {
    $("#jqGrid").Grid({
        url: '../modulesp/list',
        colModel: [
            {label: '系统编码', name: 'partCode', index: 'PART_CODE', width: 60},
            {label: '备件名称', name: 'name', index: 'NAME'},
            {label: '图号', name: 'mapNo', index: 'MAP_NO'},
            {label: '机型', name: 'type', index: 'TYPE', width: 80},
            {label: '条码代码', name: 'uniqueBarCode', index: 'UNIQUE_BAR_CODE', width: 60},
            {label: '单位', name: 'unit', index: 'UNIT', width: 40},
            {label: '备注', name: 'remark', index: 'REMARK', width: 120},
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
        ]
    });
    // 获取型芯单位下拉项列表值
    vm.getSparePartUnit();
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        moduleSp: {
            remark: '',
            uniqueBarCode: '',
            type: '',
            mapNo: '',
            name: '',
            id: '',
        },
        ruleValidate: {
            name: [
                {required: true, message: '备件名称必填', trigger: 'blur'}
            ],
            mapNo: [
                {required: true, message: '图号必填', trigger: 'blur'}
            ],
            type: [
                {required: true, message: '类别必填', trigger: 'blur'}
            ],
            uniqueBarCode: [
                {required: true, message: '条码代码必填', trigger: 'blur'}
            ]
        },
        q: {
            name: '',
            uniqueBarCode: ''
        },
        partUnitList: [], // 备件单位列表，用于新增或修改备件时单位的下拉选择
    },
    methods: {
        query: function () {
            vm.reload();
        },
        getSparePartUnit: function () {
            // 获取备件单位
            Ajax.request({
                url: '../sparepart/listUnit',
                async: false,
                successCallback: function (r) {
                    vm.partUnitList = r.sparePartUnits;
                }
            });
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.moduleSp = {
                remark: '',
                uniqueBarCode: '',
                type: '',
                mapNo: '',
                name: '',
                id: '',
            };
        },
        update: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            let url = vm.moduleSp.id == (null || '') ? "../modulesp/save" : "../modulesp/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.moduleSp),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }
            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../modulesp/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../modulesp/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.moduleSp = r.moduleSp;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name, 'uniqueBarCode': vm.q.uniqueBarCode},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: '',
                uniqueBarCode: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        handleBeforeUpload: function () {
            this.$refs.uploadFile.clearFiles()
            this.$Loading.start();
        },
        handleSuccess: function (res, file) {
            if (res.code == 0) {
                this.$Loading.finish();
                alert(res.msg, function (index) {
                    vm.reload();
                });
            } else {
                this.$Loading.error();
                this.$Notice.error({
                    title: '导入失败',
                    desc: res.msg
                })
            }
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传.xls或.xlsx的文件。'
            });
        },
        handleError: function (err, file) {
            alert('导入失败');
        },
        //数据导出
        exportData: function () {
            window.open('../modulesp/export');
        }
    }
});
